from django_redis import get_redis_connection
from rest_framework.generics import ListAPIView, RetrieveAPIView
from rest_framework.permissions import IsAuthenticated
from rest_framework.response import Response
from rest_framework.views import APIView

from activities.models import Gathering
from activities.serializer import ActivitiesSerializer, ActivitiesDetailSerializer


class ActivitiesView(ListAPIView):
    '''获取活动视图类'''
    serializer_class = ActivitiesSerializer
    queryset = Gathering.objects.all()


class ActivitiesDetailView(RetrieveAPIView):
    '''获取详细活动类'''
    serializer_class = ActivitiesDetailSerializer
    queryset = Gathering.objects.all()


class JoinActivitiesView(APIView):
    '''用户加入活动类视图'''

    permission_classes = [IsAuthenticated]

    def post(self, request, pk):
        redis_conn = get_redis_connection('activities')
        # ids = redis_conn.smembers('act_%s' % request.user.id)
        redis_conn.sadd('act_%s' % request.user.id, pk)
        return Response({'success': True,
                         'message': 'ok'})
